package com.xhsj.user.hr.system.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhsj.user.hr.base.BaseParam;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.process.processDefinition.query.MapQuery;
import com.xhsj.user.hr.system.entity.TbHrZhaopinDemandApplyPosition;
import com.xhsj.user.hr.system.mapper.TbGlobalOrganizationMapper;
import com.xhsj.user.hr.system.mapper.TbHrZhaopinDemandApplyPositionMapper;
import com.xhsj.user.hr.system.query.FindDemandApplyAllDataQuery;
import com.xhsj.user.hr.system.vo.ZhaopinDemandApplyAuditTaskVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;


import com.xhsj.user.hr.system.service.TbHrZhaopinDemandApplyService;
import com.xhsj.user.hr.system.entity.TbHrZhaopinDemandApply;


/**
 * <p>
 * 招聘需求申请表 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since Sep 3, 2020 4:18:20 PM
 */
@Slf4j
@Api(tags = "招聘需求申请表")
@RestController
@RequestMapping("/system/tbHrZhaopinDemandApply")
public class TbHrZhaopinDemandApplyController {

	@Autowired
    public TbHrZhaopinDemandApplyService service;

	@Autowired
	private TbGlobalOrganizationMapper tbGlobalOrganizationMapper;

	@Autowired
	private TbHrZhaopinDemandApplyPositionMapper tbHrZhaopinDemandApplyPositionMapper;


	/**
	 * 保存/修改
	 * @author suxiaolin
	 * @date Sep 3, 2020 4:18:20 PM
	 */
	@ApiOperation(value = "招聘需求申请表,保存/修改", notes = "招聘需求申请表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:edit')")
	public Message save(@RequestBody TbHrZhaopinDemandApply data){
    	try {
    		service.saveData(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,e.getMessage());
		}
	}

	/**
	 * 重新提交
	 * @author 苏小林
	 * @date 2020/9/7 14:04
	 */
	@ApiOperation(value = "招聘需求申请表,保存/修改", notes = "招聘需求申请表,保存和修改方法")
	@RequestMapping(value = "/resubmit", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:edit')")
	public Message resubmit(@RequestBody TbHrZhaopinDemandApply data) {

		try {
			service.resubmit(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}

	}


	/**
	 * 招聘需求申请流程审核
	 * @author 苏小林
	 * @date 2020/9/4 15:51
	 */
	@ApiOperation(value = "招聘需求申请流程审核", notes = "招聘需求申请流程审核")
	@RequestMapping(value = "/processAudit", method = RequestMethod.POST)
	public Message processAudit(@RequestBody HandleAuditDto handleAuditDto, BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}

		try {
			service.processAudit(handleAuditDto);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}

	}

	/**
	 * 通过id删除数据
	 * @author suxiaolin
	 * @date Sep 3, 2020 4:18:20 PM
	 */
	@ApiOperation(value = "招聘需求申请表删除", notes = "通过id删除招聘需求申请表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author suxiaolin
	 * @date Sep 3, 2020 4:18:20 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author suxiaolin
    * @date Sep 3, 2020 4:18:20 PM
    */
	@ApiOperation(value = "分页查询招聘需求申请表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:view')")
	public Message findList(@RequestBody BaseParam data) {
        try {
//        	IPage<TbHrZhaopinDemandApply> selectPage = service.selectPage(data);

			IPage<TbHrZhaopinDemandApply> selectPage = service.findDemandApplyList(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

	/**
	 * 查询所有数据
	 * @author suxiaolin
	 * @date Sep 3, 2020 4:18:20 PM
	 */
	@ApiOperation(value = "查询招聘需求申请表所有数据list", notes = "查询招聘需求申请表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:all:data:view')")
	public Message findAll(@RequestBody FindDemandApplyAllDataQuery data){
		try {
//        	IPage<TbHrZhaopinDemandApply> selectPage = service.selectPage(data);

			IPage<TbHrZhaopinDemandApply> selectPage = service.findAllData(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询招聘需求申请待审核任务
	 * @author 苏小林
	 * @date 2020/9/4 13:22
	 */
	@ApiOperation(value = "查询招聘需求申请待审核任务", notes = "查询招聘需求申请待审核任务")
	@RequestMapping(value="/findZhaopinDemandApplyRunTask",method = RequestMethod.POST)
	public Message findZhaopinDemandApplyRunTask(@RequestBody BaseParam baseParam) {
		try {
			IPage<ZhaopinDemandApplyAuditTaskVo> iPage = service.findZhaopinDemandApplyRunTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询招聘需求申请待历史任务
	 * @author 苏小林
	 * @date 2020/9/4 17:43
	 * @param: baseParam
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "查询招聘需求申请待历史任务", notes = "查询招聘需求申请待历史任务")
	@RequestMapping(value="/findZhaopinDemandApplyHiTask",method = RequestMethod.POST)
	public Message findZhaopinDemandApplyHiTask(@RequestBody BaseParam baseParam) {
		try {
			IPage<ZhaopinDemandApplyAuditTaskVo> iPage = service.findZhaopinDemandApplyHiTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}


	/**
	 * 通过部门查询职位
	 * @author 苏小林
	 * @date 2021/3/31 10:36
	 * @param: deptId
	 * @return: com.xhsj.user.utils.Message
	 */
	@GetMapping("/getPositionByDept")
	public Message getPositionByDept(@RequestParam("deptId") Long deptId) {
		try {
			Set<MapQuery> list = service.getPositionByDept(deptId);
			return Message.success("成功",list);

		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}


    /**
    * 通过id获取实体类
    * @author suxiaolin
    * @date Sep 3, 2020 4:18:20 PM
    */
	@ApiOperation(value = "通过id获取招聘需求申请表实体类", notes = "通过id获取招聘需求申请表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:view')")
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author suxiaolin
    * @date Sep 3, 2020 4:18:20 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:hr:zhaopin:demand:apply:view')")
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}


	@GetMapping("/getPositionList")
	public Message getPositionList() {
		// 所有职位
		List<MapQuery> allPositions = tbGlobalOrganizationMapper.findPositionList();

		List<TbHrZhaopinDemandApplyPosition> tbHrZhaopinDemandApplyPositions = tbHrZhaopinDemandApplyPositionMapper.selectList(new QueryWrapper<>());
		for (TbHrZhaopinDemandApplyPosition tbHrZhaopinDemandApplyPosition : tbHrZhaopinDemandApplyPositions) {
			MapQuery mapQuery = new MapQuery();
			mapQuery.setValue(tbHrZhaopinDemandApplyPosition.getPositionId());
			mapQuery.setLabel(tbHrZhaopinDemandApplyPosition.getName());
			allPositions.add(mapQuery);
		}

		HashSet<MapQuery> mapQueries = new HashSet<>(allPositions);
		return Message.success("成功",mapQueries);
	}

	/**
	 * 处理作废
	 * @author 苏小林
	 * @date 2021/3/31 11:27
	 * @return: com.xhsj.user.utils.Message
	 */
	@GetMapping("/handleInvalid")
	public Message handleInvalid(@Param("processTaskId") Long processTaskId,@Param("dataId") Long dataId,@Param("businessId") Long businessId) {

		try {
			service.handleInvalid(processTaskId,dataId,businessId);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("作废出错{}",e);
			return Message.error();
		}

	}

}

